package com.shop2cn.iapi.sqdistributor.testcase;

import java.util.HashMap;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.sqdistributor.parameter.request.BindDistributorConsumerRelationReq;
import com.shop2cn.iapi.sqdistributor.service.BindDistributorConsumerRelationCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.shenqi.SqDistributorService;
import com.ymttest.business.service.shenqi.bean.ShenqiUserSet;
import com.ymttest.common.order.userquery.util.Utils;
import com.ymttest.database.model.sqdistributor.DistributionAgencyCommisionRate;
import com.ymttest.database.model.sqdistributor.DistributionConfig;
import com.ymttest.database.model.sqdistributor.DistributorConsumerRelation;
import com.ymttest.database.model.sqdistributor.DistributorConsumerWechatRelation;
import com.ymttest.database.model.sqdistributor.DistributorRelation;
import com.ymttest.database.model.sqdistributor.UserCpsEvent;
import com.ymttest.database.sqlwapper.sqdistributor.DistributionAgencyCommisionRateWrapper;
import com.ymttest.database.sqlwapper.sqdistributor.DistributionAgencyConfigWrapper;
import com.ymttest.database.sqlwapper.sqdistributor.DistributionConfigWrapper;
import com.ymttest.database.sqlwapper.sqdistributor.DistributorConsumerRelationWrapper;
import com.ymttest.database.sqlwapper.sqdistributor.DistributorConsumerWechatRelationWrapper;
import com.ymttest.database.sqlwapper.sqdistributor.DistributorRelationWrapper;
import com.ymttest.database.sqlwapper.sqdistributor.UserCpsEventWrapper;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;

public class Ts_BindDistributorConsumerRelation {

	// java接口
	private static BindDistributorConsumerRelationReq bindDistributorConsumerRelationReq;
	private static BindDistributorConsumerRelationCall bindDistributorConsumerRelationCall;

	private static DistributorRelationWrapper distributorRelationWrapper = new DistributorRelationWrapper();
	private static DistributorConsumerRelationWrapper distributorConsumerRelationWrapper = new DistributorConsumerRelationWrapper();
	private static UserCpsEventWrapper userCpsEventWrapper = new UserCpsEventWrapper();

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("绑定分销员与消费者间的关系");
	}

	@Before
	public void caseUp() {
		bindDistributorConsumerRelationReq = new BindDistributorConsumerRelationReq();
		bindDistributorConsumerRelationCall = new BindDistributorConsumerRelationCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(bindDistributorConsumerRelationCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	public void verifyDb(Boolean in24HCpsEvent) throws Exception {

		DistributorRelation distributorRelation = distributorRelationWrapper
				.selectByMerchantIdAndDistributorId(
						bindDistributorConsumerRelationReq.getMerchantId(),
						bindDistributorConsumerRelationReq.getDistributorId());

		if (distributorRelation.getDistributor_type() == 2
				|| distributorRelation.getDistributor_type() == 2001) {
			Logger.comment("验证CPS机构与用户的点击上报关系");

			UserCpsEvent userCpsEvent = userCpsEventWrapper
					.selectByUserIdAndDirstributorId(
							bindDistributorConsumerRelationReq.getUserId(),
							bindDistributorConsumerRelationReq
									.getDistributorId());

			if (userCpsEvent == null)
				userCpsEvent = userCpsEventWrapper
						.selectByOpenIdAndDirstributorId(
								bindDistributorConsumerRelationReq.getOpenId(),
								bindDistributorConsumerRelationReq
										.getDistributorId());

			// 如果24小时内上报了关系，则只允许该userid上报与同一个distributor的关系
			if (Utils.falseIfNull(in24HCpsEvent)) {

				UserCpsEvent in24UserCpsEvent = userCpsEventWrapper
						.select24HEarlistByUserId(
								bindDistributorConsumerRelationReq.getUserId(),
								YMTDateUtil
										.getBeforeOrNextHour(
												-SqDistributorService.cpsExpirationTime,
												YMTDateUtil.YMDHMSS));
				// 如果第二次上报的userId/distributor与24小时内上报的一致，则新增一条记录
				if (in24UserCpsEvent.getDistributor_id().equals(
						bindDistributorConsumerRelationReq.getDistributorId())) {

					Logger.verifyEquals(true,
							YMTDateUtil.diffSecond(YMTDateUtil.getDate(),
									userCpsEvent.getAdd_time()) < 3,
							"验证Add_time");
				} else
					Logger.verifyEquals(null, userCpsEvent,
							"24H小时内已经绑定了另外一个distributor");
			} else {

				Long mainDistributorId = distributorRelationWrapper
						.selectByMerchantIdAndDistributorId(
								bindDistributorConsumerRelationReq
										.getMerchantId(),
								bindDistributorConsumerRelationReq
										.getDistributorId())
						.getParent_distributor_id();

				Logger.verifyEquals(
						bindDistributorConsumerRelationReq.getMerchantId(),
						userCpsEvent.getMerchant_id(), "验证Merchant_id");
				Logger.verifyEquals(mainDistributorId,
						userCpsEvent.getCps_main_distributor_id(),
						"验证Cps_main_distributor_id");
			}

		} else {
			Logger.comment("验证非CPS机构，用户与机构的关系");

			Integer bindingEffectiveDays = null;

			if (distributorRelation.getDistributor_type() == 0) {

				DistributionConfigWrapper distributionConfigWrapper = new DistributionConfigWrapper();

				DistributionConfig distributionConfig = distributionConfigWrapper
						.selectByMerchantId(bindDistributorConsumerRelationReq
								.getMerchantId());

				if (distributionConfig.getUser_distributor_bind_rule_type() == 3)
					bindingEffectiveDays = distributionConfig
							.getBinding_effective_threshold();

			} else {

				DistributionAgencyConfigWrapper distributionAgencyConfigWrapper = new DistributionAgencyConfigWrapper();

				Long configId = distributionAgencyConfigWrapper
						.selectByMerchantIdAndAgencyType(
								bindDistributorConsumerRelationReq
										.getMerchantId(),
								1).getId();

				DistributionAgencyCommisionRateWrapper distributionAgencyCommisionRateWrapper = new DistributionAgencyCommisionRateWrapper();

				DistributionAgencyCommisionRate distributionAgencyCommisionRate = distributionAgencyCommisionRateWrapper
						.selectByConfigId(configId);

				if (distributionAgencyCommisionRate
						.getUser_distributor_bind_rule_type() == 3)
					bindingEffectiveDays = distributionAgencyCommisionRate
							.getBinding_effective_threshold();
			}

			DistributorConsumerWechatRelationWrapper distributorConsumerWechatRelationWrapper = new DistributorConsumerWechatRelationWrapper();

			DistributorConsumerWechatRelation distributorConsumerWechatRelation = distributorConsumerWechatRelationWrapper
					.selectByMerchantIdAndOpenId(
							bindDistributorConsumerRelationReq.getMerchantId(),
							bindDistributorConsumerRelationReq.getOpenId());

			this.verifyDistributorConsumerWechatRelationTable(
					distributorConsumerWechatRelation,
					distributorRelation.getDistributor_type(), bindingEffectiveDays);

			// 如果分销员被清退了，找到上级
			while (distributorRelation.getDistributor_status() == 1) {

				distributorRelation = distributorRelationWrapper
						.selectByMerchantIdAndDistributorId(
								bindDistributorConsumerRelationReq
										.getMerchantId(), distributorRelation
										.getParent_distributor_id());
			}

			if (0 != Utils.zeroIfNull(bindDistributorConsumerRelationReq
					.getUserId())) {

				long distributorId = distributorRelation.getDistributor_id();

				DistributorConsumerRelation actualResult = distributorConsumerRelationWrapper
						.selectByMerchantIdAndUserId(
								bindDistributorConsumerRelationReq
										.getMerchantId(),
								bindDistributorConsumerRelationReq.getUserId());

				Logger.verifyEquals(
						bindDistributorConsumerRelationReq.getMerchantId(),
						actualResult.getMerchant_id(), "验证Merchant_id");
				Logger.verifyEquals(
						bindDistributorConsumerRelationReq.getUserId(),
						actualResult.getUser_id(), "验证User_id");
				Logger.verifyEquals(
						bindDistributorConsumerRelationReq.getUserPhone(),
						actualResult.getUser_phone(), "验证User_phone");
				Logger.verifyEquals(
						bindDistributorConsumerRelationReq.getUserName(),
						actualResult.getUser_name(), "验证User_name");
				Logger.verifyEquals(distributorId,
						actualResult.getDistributor_id(), "验证Distributor_id");
				Logger.verifyEquals(false, actualResult.getIs_deleted(),
						"验证Is_deleted");

				if (bindingEffectiveDays == null)
					Logger.verifyEquals(null,
							actualResult.getExpiration_date(),
							"验证Expiration_date");
				else
					Logger.verifyEquals((long)bindingEffectiveDays, YMTDateUtil
							.diffDay(actualResult.getExpiration_date(), YMTDateUtil.getDate(
									YMTDateUtil.getBeforeOrNextDay(0),
									YMTDateUtil.YMD)), "验证Expiration_date");
			}
		}
	}

	public void verifyDistributorConsumerWechatRelationTable(
			DistributorConsumerWechatRelation actualResult,
			Integer distributorType, Integer bindingEffectiveDays) {

		Logger.comment("验证distributor_consumer_wechat_relation");

		Logger.verifyEquals(bindDistributorConsumerRelationReq.getUserId(),
				actualResult.getUser_id(), "验证User_id");
		Logger.verifyEquals(
				bindDistributorConsumerRelationReq.getDistributorId(),
				actualResult.getDistributor_id(), "验证Distributor_id");
		Logger.verifyEquals(distributorType,
				actualResult.getDistributor_type(), "验证Distributor_type");

		if (bindingEffectiveDays == null)
			Logger.verifyEquals(null, actualResult.getExpiration_date(),
					"验证Expiration_date");
		else
			Logger.verifyEquals((long) bindingEffectiveDays, YMTDateUtil
					.diffDay(actualResult.getExpiration_date(), YMTDateUtil
							.getDate(YMTDateUtil.getBeforeOrNextDay(0),
									YMTDateUtil.YMD)), "验证Expiration_date");
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_BindDistributorConsumerRelation_001() {
		Logger.start(true, "商户不存在，绑定失败");

		try {

			bindDistributorConsumerRelationReq.setDistributorId(701246162L);
			bindDistributorConsumerRelationReq.setMerchantId(24124215124L);
			bindDistributorConsumerRelationReq.setUserId((long) TestDataManager
					.getRandomIntNum(8));
			bindDistributorConsumerRelationReq.setUserName(TestDataManager
					.getChineseName());
			bindDistributorConsumerRelationReq.setUserPhone(TestDataManager
					.getPhoneNum());
			// bindDistributorConsumerRelationReq.setOpenId(TestDataManager.randomStr(10));

			bindDistributorConsumerRelationCall
					.setData(bindDistributorConsumerRelationReq);
			bindDistributorConsumerRelationCall.callService();

			Logger.verifyEquals(false,
					bindDistributorConsumerRelationCall.getSuccess(),
					"验证success");
			Logger.verifyEquals(String.format("分销员：701246162不存在！",
					bindDistributorConsumerRelationReq.getMerchantId()),
					bindDistributorConsumerRelationCall.getMsg(),
					"验证resultMessage");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_BindDistributorConsumerRelation_002() {
		Logger.start(true, "分销员不存在，绑定失败");

		try {

			bindDistributorConsumerRelationReq.setDistributorId(12412341212L);
			bindDistributorConsumerRelationReq.setMerchantId(500861041L);
			bindDistributorConsumerRelationReq.setUserId((long) TestDataManager
					.getRandomIntNum(8));
			bindDistributorConsumerRelationReq.setUserName(TestDataManager
					.getChineseName());
			bindDistributorConsumerRelationReq.setUserPhone(TestDataManager
					.getPhoneNum());
			bindDistributorConsumerRelationReq.setOpenId(TestDataManager
					.randomStr(10));

			bindDistributorConsumerRelationCall
					.setData(bindDistributorConsumerRelationReq);
			bindDistributorConsumerRelationCall.callService();

			Logger.verifyEquals(false,
					bindDistributorConsumerRelationCall.getSuccess(),
					"验证success");
			Logger.verifyEquals(String.format("分销员：%s不存在！",
					bindDistributorConsumerRelationReq.getDistributorId()),
					bindDistributorConsumerRelationCall.getMsg(),
					"验证resultMessage");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_BindDistributorConsumerRelation_003() {
		Logger.start(true, "绑定分销员与消费者，分销员为永久关系，绑定成功");

		try {

			Integer beforeBindCount = distributorRelationWrapper
					.selectByMerchantIdAndDistributorId(
							ShenqiUserSet.permanentRelationMerchantId,
							978039984L).getInvite_count();

			bindDistributorConsumerRelationReq.setDistributorId(978039984L);
			bindDistributorConsumerRelationReq
					.setMerchantId(ShenqiUserSet.permanentRelationMerchantId);
			bindDistributorConsumerRelationReq.setUserId((long) TestDataManager
					.getRandomIntNum(8));
			bindDistributorConsumerRelationReq.setUserName(TestDataManager
					.getChineseName());
			bindDistributorConsumerRelationReq.setUserPhone(TestDataManager
					.getPhoneNum());
			bindDistributorConsumerRelationReq.setOpenId(TestDataManager
					.randomStr(10));

			bindDistributorConsumerRelationCall
					.setData(bindDistributorConsumerRelationReq);
			bindDistributorConsumerRelationCall.callService();

			Integer afterBindCount = distributorRelationWrapper
					.selectByMerchantIdAndDistributorId(
							ShenqiUserSet.permanentRelationMerchantId,
							978039984L).getInvite_count();

			Logger.verifyEquals(true,
					bindDistributorConsumerRelationCall.getSuccess(),
					"验证success");
			Logger.verifyEquals(beforeBindCount + 1, afterBindCount,
					"验证Invite_count");

			verifyDb(false);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_BindDistributorConsumerRelation_004() {
		Logger.start(true, "绑定分销员与消费者，分销员为临时关系，绑定成功");

		try {

			Integer beforeBindCount = distributorRelationWrapper
					.selectByMerchantIdAndDistributorId(
							ShenqiUserSet.tempRelationMerchantId, 818100343L)
					.getInvite_count();

			bindDistributorConsumerRelationReq.setDistributorId(818100343L);
			bindDistributorConsumerRelationReq
					.setMerchantId(ShenqiUserSet.tempRelationMerchantId);
			bindDistributorConsumerRelationReq.setUserId((long) TestDataManager
					.getRandomIntNum(8));
			bindDistributorConsumerRelationReq.setUserName(TestDataManager
					.getChineseName());
			bindDistributorConsumerRelationReq.setUserPhone(TestDataManager
					.getPhoneNum());
			bindDistributorConsumerRelationReq.setOpenId(TestDataManager
					.randomStr(10));

			bindDistributorConsumerRelationCall
					.setData(bindDistributorConsumerRelationReq);
			bindDistributorConsumerRelationCall.callService();

			Integer afterBindCount = distributorRelationWrapper
					.selectByMerchantIdAndDistributorId(
							ShenqiUserSet.tempRelationMerchantId, 818100343L)
					.getInvite_count();

			Logger.verifyEquals(true,
					bindDistributorConsumerRelationCall.getSuccess(),
					"验证success");
			Logger.verifyEquals(beforeBindCount + 1, afterBindCount,
					"验证Invite_count");

			verifyDb(false);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_BindDistributorConsumerRelation_005() {
		Logger.start(true, "消费者已经绑定永久关系，再次绑定其他商户分销员，绑定失败");

		try {

			bindDistributorConsumerRelationReq.setDistributorId(1194404447L);
			bindDistributorConsumerRelationReq.setMerchantId(500861049L);
			bindDistributorConsumerRelationReq.setUserId(36747032L);
			bindDistributorConsumerRelationReq.setUserName(TestDataManager
					.getChineseName());
			bindDistributorConsumerRelationReq.setUserPhone(TestDataManager
					.getPhoneNum());
			bindDistributorConsumerRelationReq.setOpenId(TestDataManager
					.randomStr(10));

			bindDistributorConsumerRelationCall
					.setData(bindDistributorConsumerRelationReq);
			bindDistributorConsumerRelationCall.callService();

			Logger.verifyEquals(false,
					bindDistributorConsumerRelationCall.getSuccess(),
					"验证success");
			Logger.verifyEquals(String.format("分销员：%s与用户%s关系绑定失败！",
					bindDistributorConsumerRelationReq.getDistributorId(),
					bindDistributorConsumerRelationReq.getUserId()),
					bindDistributorConsumerRelationCall.getMsg(),
					"验证resultMessage");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_BindDistributorConsumerRelation_006() {
		Logger.start(true, "消费者已经绑定临时关系，再次绑定其他分销员，绑定成功");

		try {

			Long userId = (long) TestDataManager.getRandomIntNum(8);

			BindDistributorConsumerRelationReq tempRelation = SqDistributorService
					.bindDistributorConsumerTempRelation(userId);
			
			Thread.sleep(3000);

			bindDistributorConsumerRelationReq.setDistributorId(26278552L);
			bindDistributorConsumerRelationReq.setMerchantId(500002490L);
			bindDistributorConsumerRelationReq.setUserId(userId);
			bindDistributorConsumerRelationReq.setUserName(tempRelation
					.getUserName());
			bindDistributorConsumerRelationReq.setUserPhone(tempRelation
					.getUserPhone());
			bindDistributorConsumerRelationReq.setOpenId(TestDataManager
					.randomStr(10));

			bindDistributorConsumerRelationCall
					.setData(bindDistributorConsumerRelationReq);
			bindDistributorConsumerRelationCall.callService();

			Logger.verifyEquals(true,
					bindDistributorConsumerRelationCall.getSuccess(),
					"验证success");

			verifyDb(false);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_BindDistributorConsumerRelation_007() {
		Logger.start(true, "DistributorId为null，绑定失败");

		try {

			// bindDistributorConsumerRelationReq.setDistributorId(60792707L);
			bindDistributorConsumerRelationReq.setMerchantId(500861041L);
			bindDistributorConsumerRelationReq.setUserId((long) TestDataManager
					.getRandomIntNum(8));
			bindDistributorConsumerRelationReq.setUserName(TestDataManager
					.getChineseName());
			bindDistributorConsumerRelationReq.setUserPhone(TestDataManager
					.getPhoneNum());
			// bindDistributorConsumerRelationReq.setOpenId(TestDataManager.randomStr(10));

			bindDistributorConsumerRelationCall
					.setData(bindDistributorConsumerRelationReq);
			bindDistributorConsumerRelationCall.callService();

			Logger.verifyEquals(false,
					bindDistributorConsumerRelationCall.getSuccess(),
					"验证success");
			Logger.verifyEquals(
					"distributorId:must be greater than or equal to 1",
					bindDistributorConsumerRelationCall.getMsg(),
					"验证resultMessage");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_BindDistributorConsumerRelation_008() {
		Logger.start(true, "MerchantId为null，绑定失败");

		try {

			bindDistributorConsumerRelationReq.setDistributorId(60792707L);
			// bindDistributorConsumerRelationReq.setMerchantId(500861041L);
			bindDistributorConsumerRelationReq.setUserId((long) TestDataManager
					.getRandomIntNum(8));
			bindDistributorConsumerRelationReq.setUserName(TestDataManager
					.getChineseName());
			bindDistributorConsumerRelationReq.setUserPhone(TestDataManager
					.getPhoneNum());
			bindDistributorConsumerRelationReq.setOpenId(TestDataManager
					.randomStr(10));

			bindDistributorConsumerRelationCall
					.setData(bindDistributorConsumerRelationReq);
			bindDistributorConsumerRelationCall.callService();

			Logger.verifyEquals(false,
					bindDistributorConsumerRelationCall.getSuccess(),
					"验证success");
			Logger.verifyEquals(
					"merchantId:must be greater than or equal to 1",
					bindDistributorConsumerRelationCall.getMsg(),
					"验证resultMessage");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_BindDistributorConsumerRelation_009() {
		Logger.start(true, "UserId为null，绑定失败");

		try {

			bindDistributorConsumerRelationReq.setDistributorId(60792707L);
			bindDistributorConsumerRelationReq.setMerchantId(500000795L);
			// bindDistributorConsumerRelationReq.setUserId((long)TestDataManager.getRandomIntNum(8));
			bindDistributorConsumerRelationReq.setUserName(TestDataManager
					.getChineseName());
			bindDistributorConsumerRelationReq.setUserPhone(TestDataManager
					.getPhoneNum());
			// bindDistributorConsumerRelationReq.setOpenId(TestDataManager.randomStr(10));

			bindDistributorConsumerRelationCall
					.setData(bindDistributorConsumerRelationReq);
			bindDistributorConsumerRelationCall.callService();

			Logger.verifyEquals(false,
					bindDistributorConsumerRelationCall.getSuccess(),
					"验证success");
			Logger.verifyEquals("userId can't be null",
					bindDistributorConsumerRelationCall.getMsg(),
					"验证resultMessage");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_BindDistributorConsumerRelation_012() {
		Logger.start(true, "分销员已经被清退，找到一个最近的上级，绑定成功");

		try {

			bindDistributorConsumerRelationReq.setDistributorId(86417205L);
			bindDistributorConsumerRelationReq.setMerchantId(500861050L);
			bindDistributorConsumerRelationReq.setUserId((long) TestDataManager
					.getRandomIntNum(8));
			bindDistributorConsumerRelationReq.setUserName(TestDataManager
					.getChineseName());
			bindDistributorConsumerRelationReq.setUserPhone(TestDataManager
					.getPhoneNum());
			bindDistributorConsumerRelationReq.setOpenId(TestDataManager
					.randomStr(10));

			bindDistributorConsumerRelationCall
					.setData(bindDistributorConsumerRelationReq);
			bindDistributorConsumerRelationCall.callService();

			Logger.verifyEquals(true,
					bindDistributorConsumerRelationCall.getSuccess(),
					"验证success");

			verifyDb(false);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_BindDistributorConsumerRelation_013() {
		Logger.start(true, "用户与商户之间的永久关系被删除之后，可再次绑定");

		try {

			Long userId = (long) TestDataManager.getRandomIntNum(8);

			SqDistributorService.bindDistributorConsumerForeverRelation(userId);

			Long id = new DistributorConsumerRelationWrapper()
					.selectByMerchantIdAndUserId(500861041L, userId).getId();

			SqDistributorService.deleteDistributorConsumerRelation(id);

			bindDistributorConsumerRelationReq.setDistributorId(86417205L);
			bindDistributorConsumerRelationReq.setMerchantId(500861050L);
			bindDistributorConsumerRelationReq.setUserId(userId);
			bindDistributorConsumerRelationReq.setUserName(TestDataManager
					.getChineseName());
			bindDistributorConsumerRelationReq.setUserPhone(TestDataManager
					.getPhoneNum());
			bindDistributorConsumerRelationReq.setOpenId(TestDataManager
					.randomStr(10));

			bindDistributorConsumerRelationCall
					.setData(bindDistributorConsumerRelationReq);
			bindDistributorConsumerRelationCall.callService();

			Logger.verifyEquals(true,
					bindDistributorConsumerRelationCall.getSuccess(),
					"验证success");

			verifyDb(false);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_BindDistributorConsumerRelation_014() {
		Logger.start(true, "绑定分销员与消费者，分销员为过期时间关系，绑定成功");

		try {

			Integer beforeBindCount = distributorRelationWrapper
					.selectByMerchantIdAndDistributorId(
							ShenqiUserSet.indateRelationMerchantId, 703010695L)
					.getInvite_count();

			bindDistributorConsumerRelationReq.setDistributorId(703010695L);
			bindDistributorConsumerRelationReq
					.setMerchantId(ShenqiUserSet.indateRelationMerchantId);
			bindDistributorConsumerRelationReq.setUserId((long) TestDataManager
					.getRandomIntNum(8));
			bindDistributorConsumerRelationReq.setUserName(TestDataManager
					.getChineseName());
			bindDistributorConsumerRelationReq.setUserPhone(TestDataManager
					.getPhoneNum());
			bindDistributorConsumerRelationReq.setOpenId(TestDataManager
					.randomStr(10));

			bindDistributorConsumerRelationCall
					.setData(bindDistributorConsumerRelationReq);
			bindDistributorConsumerRelationCall.callService();

			Integer afterBindCount = distributorRelationWrapper
					.selectByMerchantIdAndDistributorId(
							ShenqiUserSet.indateRelationMerchantId, 703010695L)
					.getInvite_count();

			Logger.verifyEquals(true,
					bindDistributorConsumerRelationCall.getSuccess(),
					"验证success");
			Logger.verifyEquals(beforeBindCount + 1, afterBindCount,
					"验证Invite_count");

			verifyDb(false);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_BindDistributorConsumerRelation_015() {
		Logger.start(true, "绑定分销员与消费者，消费者已经和这个商户的其他分销员绑定，未过期，再次绑定另外的分销员，绑定失败");

		try {

			Long userId = (long) TestDataManager.getRandomIntNum(8);

			SqDistributorService.bindDistributorConsumerRelation(userId,
					ShenqiUserSet.indateRelationMerchantId, 788621836L);

			bindDistributorConsumerRelationReq.setDistributorId(404660516L);
			bindDistributorConsumerRelationReq
					.setMerchantId(ShenqiUserSet.indateRelationMerchantId);
			bindDistributorConsumerRelationReq.setUserId(userId);
			bindDistributorConsumerRelationReq.setUserName(TestDataManager
					.getChineseName());
			bindDistributorConsumerRelationReq.setUserPhone(TestDataManager
					.getPhoneNum());
			bindDistributorConsumerRelationReq.setOpenId(TestDataManager
					.randomStr(10));

			bindDistributorConsumerRelationCall
					.setData(bindDistributorConsumerRelationReq);
			bindDistributorConsumerRelationCall.callService();

			Logger.verifyEquals(false,
					bindDistributorConsumerRelationCall.getSuccess(),
					"验证success");
			Logger.verifyEquals(
					String.format("分销员：404660516与用户%s关系绑定失败！", userId),
					bindDistributorConsumerRelationCall.getMsg(),
					"验证resultMessage");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_BindDistributorConsumerRelation_016() {
		Logger.start(true, "绑定分销员与消费者，消费者已经和这个商户的其他分销员绑定，已经过期，再次绑定另外的分销员，绑定成功");

		try {

			Long userId = (long) TestDataManager.getRandomIntNum(8);

			BindDistributorConsumerRelationReq tempBindDistributorConsumerRelationReq = SqDistributorService
					.bindDistributorConsumerRelation(userId,
							ShenqiUserSet.indateRelationMerchantId, 788621836L);

			// 将消费者与分销员绑定关系置为过期
			distributorConsumerRelationWrapper.updateExpirationDate(userId,
					788621836L);

			bindDistributorConsumerRelationReq
					.setDistributorId(ShenqiUserSet.indateRelationMerchantId);
			bindDistributorConsumerRelationReq
					.setMerchantId(ShenqiUserSet.indateRelationMerchantId);
			bindDistributorConsumerRelationReq.setUserId(userId);
			bindDistributorConsumerRelationReq
					.setUserName(tempBindDistributorConsumerRelationReq
							.getUserName());
			bindDistributorConsumerRelationReq
					.setUserPhone(tempBindDistributorConsumerRelationReq
							.getUserPhone());
			bindDistributorConsumerRelationReq.setOpenId(TestDataManager
					.randomStr(10));

			bindDistributorConsumerRelationCall
					.setData(bindDistributorConsumerRelationReq);
			bindDistributorConsumerRelationCall.callService();

			Logger.verifyEquals(true,
					bindDistributorConsumerRelationCall.getSuccess(),
					"验证success");

			verifyDb(false);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_BindDistributorConsumerRelation_017() {
		Logger.start(true, "消费者已经绑定了客服，绑定分销员与消费者，允许绑定");

		try {

			HashMap<String, Object> map = SqDistributorService
					.createSelfPurchaseUser(
							ShenqiUserSet.indateRelationMerchantId, true);

			Long userId = ((Integer) map.get("userId")).longValue();

			bindDistributorConsumerRelationReq.setDistributorId(703010695L);
			bindDistributorConsumerRelationReq
					.setMerchantId(ShenqiUserSet.indateRelationMerchantId);
			bindDistributorConsumerRelationReq.setUserId(userId);
			bindDistributorConsumerRelationReq.setUserName(TestDataManager
					.getChineseName());
			bindDistributorConsumerRelationReq.setUserPhone((String) map.get("mobile"));
			bindDistributorConsumerRelationReq.setOpenId(TestDataManager
					.randomStr(10));

			bindDistributorConsumerRelationCall
					.setData(bindDistributorConsumerRelationReq);
			bindDistributorConsumerRelationCall.callService();

			Logger.verifyEquals(true,
					bindDistributorConsumerRelationCall.getSuccess(),
					"验证success");

			verifyDb(false);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_BindDistributorConsumerRelation_018() {
		Logger.start(true, "绑定CPS与消费者，添加点击事件成功");

		try {

			bindDistributorConsumerRelationReq.setDistributorId(137984927L);
			bindDistributorConsumerRelationReq.setMerchantId(500865146L);
			bindDistributorConsumerRelationReq.setUserId((long) TestDataManager
					.getRandomIntNum(8));
			bindDistributorConsumerRelationReq.setOpenId(TestDataManager
					.randomStr(10));

			bindDistributorConsumerRelationCall
					.setData(bindDistributorConsumerRelationReq);
			bindDistributorConsumerRelationCall.callService();

			Logger.verifyEquals(true,
					bindDistributorConsumerRelationCall.getSuccess(),
					"验证success");

			verifyDb(false);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_BindDistributorConsumerRelation_019() {
		Logger.start(true,
				"绑定分销员与消费者，消费者已经和这个商户的其他分销员绑定，已经过期，再次绑定分销机构的分销员，绑定成功");

		try {

			// 创建一个新的用户和分销员，并绑定
			Long userId = (long) TestDataManager.getRandomIntNum(8);

			BindDistributorConsumerRelationReq tempBindDistributorConsumerRelationReq = SqDistributorService
					.bindDistributorConsumerRelation(userId,
							ShenqiUserSet.indateRelationMerchantId, 788621836L);

			// 将消费者与分销员绑定关系置为过期
			distributorConsumerRelationWrapper.updateExpirationDate(userId,
					ShenqiUserSet.indateRelationMerchantId);

			bindDistributorConsumerRelationReq
					.setDistributorId(ShenqiUserSet.agencyDistributorId);
			bindDistributorConsumerRelationReq
					.setMerchantId(ShenqiUserSet.agencyMerchantId);
			bindDistributorConsumerRelationReq.setUserId(userId);
			bindDistributorConsumerRelationReq
					.setUserName(tempBindDistributorConsumerRelationReq
							.getUserName());
			bindDistributorConsumerRelationReq
					.setUserPhone(tempBindDistributorConsumerRelationReq
							.getUserPhone());
			bindDistributorConsumerRelationReq.setOpenId(TestDataManager
					.randomStr(10));

			bindDistributorConsumerRelationCall
					.setData(bindDistributorConsumerRelationReq);
			bindDistributorConsumerRelationCall.callService();

			Logger.verifyEquals(true,
					bindDistributorConsumerRelationCall.getSuccess(),
					"验证success");

			verifyDb(false);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	@Ignore
	// CPS已经取消
	public void Tc_BindDistributorConsumerRelation_020() {
		Logger.start(true, "24小时内，已经绑定了CPS，再次绑定另外CPS与消费者，无法绑定");

		try {

			long userId = (long) TestDataManager.getRandomIntNum(8);

			// 第一次绑定
			bindDistributorConsumerRelationReq.setDistributorId(624037847L);
			bindDistributorConsumerRelationReq.setMerchantId(500865146L);
			bindDistributorConsumerRelationReq.setUserId(userId);
			bindDistributorConsumerRelationReq.setOpenId(TestDataManager
					.randomStr(10));

			bindDistributorConsumerRelationCall
					.setData(bindDistributorConsumerRelationReq);
			bindDistributorConsumerRelationCall.callService();

			// 第二次绑定
			bindDistributorConsumerRelationReq.setDistributorId(137984927L);
			bindDistributorConsumerRelationReq.setMerchantId(500865146L);
			bindDistributorConsumerRelationReq.setUserId(userId);
			bindDistributorConsumerRelationReq.setOpenId(TestDataManager
					.randomStr(10));

			bindDistributorConsumerRelationCall
					.setData(bindDistributorConsumerRelationReq);
			bindDistributorConsumerRelationCall.callService();

			Logger.verifyEquals(true,
					bindDistributorConsumerRelationCall.getSuccess(),
					"验证success");

			verifyDb(true);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	@Ignore
	public void Tc_BindDistributorConsumerRelation_021() {
		Logger.start(true, "24小时内，已经绑定了CPS，再次绑定同一个distributor与消费者，可以绑定");

		try {

			long userId = (long) TestDataManager.getRandomIntNum(8);

			// 第一次绑定
			bindDistributorConsumerRelationReq.setDistributorId(624037847L);
			bindDistributorConsumerRelationReq.setMerchantId(500865146L);
			bindDistributorConsumerRelationReq.setUserId(userId);
			bindDistributorConsumerRelationReq.setOpenId(TestDataManager
					.randomStr(10));

			bindDistributorConsumerRelationCall
					.setData(bindDistributorConsumerRelationReq);
			bindDistributorConsumerRelationCall.callService();

			Thread.sleep(3000);

			// 第二次绑定
			bindDistributorConsumerRelationReq.setDistributorId(624037847L);
			bindDistributorConsumerRelationReq.setMerchantId(500865146L);
			bindDistributorConsumerRelationReq.setUserId(userId);
			bindDistributorConsumerRelationReq.setOpenId(TestDataManager
					.randomStr(10));

			bindDistributorConsumerRelationCall
					.setData(bindDistributorConsumerRelationReq);
			bindDistributorConsumerRelationCall.callService();

			Logger.verifyEquals(true,
					bindDistributorConsumerRelationCall.getSuccess(),
					"验证success");

			verifyDb(true);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	@Ignore
	public void Tc_BindDistributorConsumerRelation_022() {
		Logger.start(true, "24小时外，已经绑定了CPS，再次绑定另外CPS与消费者，验证数据");

		try {

			long userId = (long) TestDataManager.getRandomIntNum(8);

			// 第一次绑定
			bindDistributorConsumerRelationReq.setDistributorId(624037847L);
			bindDistributorConsumerRelationReq.setMerchantId(500865146L);
			bindDistributorConsumerRelationReq.setUserId(userId);
			bindDistributorConsumerRelationReq.setOpenId(TestDataManager
					.randomStr(10));

			bindDistributorConsumerRelationCall
					.setData(bindDistributorConsumerRelationReq);
			bindDistributorConsumerRelationCall.callService();

			// 修改点击事件为30天前
			userCpsEventWrapper.updateAddTimeByUserIdAndDistributorId(userId,
					bindDistributorConsumerRelationReq.getDistributorId());

			// 第二次绑定
			bindDistributorConsumerRelationReq.setDistributorId(137984927L);
			bindDistributorConsumerRelationReq.setMerchantId(500865146L);
			bindDistributorConsumerRelationReq.setUserId(userId);
			bindDistributorConsumerRelationReq.setOpenId(TestDataManager
					.randomStr(10));

			bindDistributorConsumerRelationCall
					.setData(bindDistributorConsumerRelationReq);
			bindDistributorConsumerRelationCall.callService();

			Logger.verifyEquals(true,
					bindDistributorConsumerRelationCall.getSuccess(),
					"验证success");

			verifyDb(false);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_BindDistributorConsumerRelation_023() {
		Logger.start(true, "openId与分销机构永久绑定，验证数据");

		try {

			// 第一次绑定
			bindDistributorConsumerRelationReq
					.setDistributorId(ShenqiUserSet.agencyDistributorId);
			bindDistributorConsumerRelationReq
					.setMerchantId(ShenqiUserSet.agencyMerchantId);
			bindDistributorConsumerRelationReq.setOpenId(TestDataManager
					.randomStr(10));

			bindDistributorConsumerRelationCall
					.setData(bindDistributorConsumerRelationReq);
			bindDistributorConsumerRelationCall.callService();

			Logger.verifyEquals(true,
					bindDistributorConsumerRelationCall.getSuccess(),
					"验证success");

			verifyDb(false);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_BindDistributorConsumerRelation_024() {
		Logger.start(true, "openId与分销员临时绑定，验证数据");

		try {

			// 第一次绑定
			bindDistributorConsumerRelationReq.setDistributorId(818100343L);
			bindDistributorConsumerRelationReq
					.setMerchantId(ShenqiUserSet.tempRelationMerchantId);
			bindDistributorConsumerRelationReq.setOpenId(TestDataManager
					.randomStr(10));

			bindDistributorConsumerRelationCall
					.setData(bindDistributorConsumerRelationReq);
			bindDistributorConsumerRelationCall.callService();

			Logger.verifyEquals(true,
					bindDistributorConsumerRelationCall.getSuccess(),
					"验证success");

			verifyDb(false);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_BindDistributorConsumerRelation_025() {
		Logger.start(true, "openId与分销员绑定有效期关系，验证数据");

		try {

			// 第一次绑定
			bindDistributorConsumerRelationReq.setDistributorId(404660516L);
			bindDistributorConsumerRelationReq
					.setMerchantId(ShenqiUserSet.indateRelationMerchantId);
			bindDistributorConsumerRelationReq.setOpenId(TestDataManager
					.randomStr(10));

			bindDistributorConsumerRelationCall
					.setData(bindDistributorConsumerRelationReq);
			bindDistributorConsumerRelationCall.callService();

			Logger.verifyEquals(true,
					bindDistributorConsumerRelationCall.getSuccess(),
					"验证success");

			verifyDb(false);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_BindDistributorConsumerRelation_026() {
		Logger.start(true, "openId与分销机构永久绑定，再次使用userId绑定，验证数据");

		try {

			// 第一次绑定
			bindDistributorConsumerRelationReq
					.setDistributorId(ShenqiUserSet.agencyDistributorId);
			bindDistributorConsumerRelationReq
					.setMerchantId(ShenqiUserSet.agencyMerchantId);
			bindDistributorConsumerRelationReq.setOpenId(TestDataManager
					.randomStr(10));

			bindDistributorConsumerRelationCall
					.setData(bindDistributorConsumerRelationReq);
			bindDistributorConsumerRelationCall.callService();

			Thread.sleep(3000);

			// 第二次绑定
			bindDistributorConsumerRelationReq.setUserId((long) TestDataManager
					.getRandomIntNum(9));

			bindDistributorConsumerRelationCall
					.setData(bindDistributorConsumerRelationReq);
			bindDistributorConsumerRelationCall.callService();

			Logger.verifyEquals(true,
					bindDistributorConsumerRelationCall.getSuccess(),
					"验证success");

			verifyDb(false);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_BindDistributorConsumerRelation_027() {
		Logger.start(true, "openId与分销员临时绑定，再次使用userId, 绑定验证数据");

		try {

			// 第一次绑定
			bindDistributorConsumerRelationReq.setDistributorId(818100343L);
			bindDistributorConsumerRelationReq
					.setMerchantId(ShenqiUserSet.tempRelationMerchantId);
			bindDistributorConsumerRelationReq.setOpenId(TestDataManager
					.randomStr(10));

			bindDistributorConsumerRelationCall
					.setData(bindDistributorConsumerRelationReq);
			bindDistributorConsumerRelationCall.callService();

			Thread.sleep(3000);

			// 第二次绑定
			bindDistributorConsumerRelationReq.setUserId((long) TestDataManager
					.getRandomIntNum(9));

			bindDistributorConsumerRelationCall
					.setData(bindDistributorConsumerRelationReq);
			bindDistributorConsumerRelationCall.callService();

			Logger.verifyEquals(true,
					bindDistributorConsumerRelationCall.getSuccess(),
					"验证success");

			verifyDb(false);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_BindDistributorConsumerRelation_028() {
		Logger.start(true, "openId与分销员绑定有效期关系，再次使用userId绑定，验证数据");

		try {

			// 第一次绑定
			bindDistributorConsumerRelationReq.setDistributorId(404660516L);
			bindDistributorConsumerRelationReq
					.setMerchantId(ShenqiUserSet.indateRelationMerchantId);
			bindDistributorConsumerRelationReq.setOpenId(TestDataManager
					.randomStr(10));

			bindDistributorConsumerRelationCall
					.setData(bindDistributorConsumerRelationReq);
			bindDistributorConsumerRelationCall.callService();

			Thread.sleep(3000);

			// 第二次绑定
			bindDistributorConsumerRelationReq.setUserId((long) TestDataManager
					.getRandomIntNum(9));

			bindDistributorConsumerRelationCall
					.setData(bindDistributorConsumerRelationReq);
			bindDistributorConsumerRelationCall.callService();

			Logger.verifyEquals(true,
					bindDistributorConsumerRelationCall.getSuccess(),
					"验证success");

			verifyDb(false);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_BindDistributorConsumerRelation_029() {
		Logger.start(true, "openId与CPS绑定关系，再次使用userId绑定，添加点击事件成功");

		try {

			bindDistributorConsumerRelationReq.setDistributorId(137984927L);
			bindDistributorConsumerRelationReq.setMerchantId(500865146L);
			bindDistributorConsumerRelationReq.setOpenId(TestDataManager
					.randomStr(10));

			bindDistributorConsumerRelationCall
					.setData(bindDistributorConsumerRelationReq);
			bindDistributorConsumerRelationCall.callService();

			Thread.sleep(3000);

			// 第二次绑定
			bindDistributorConsumerRelationReq.setUserId((long) TestDataManager
					.getRandomIntNum(9));

			bindDistributorConsumerRelationCall
					.setData(bindDistributorConsumerRelationReq);
			bindDistributorConsumerRelationCall.callService();

			Logger.verifyEquals(true,
					bindDistributorConsumerRelationCall.getSuccess(),
					"验证success");

			verifyDb(false);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	public static void main(String[] args) {
		System.out.println(YMTDateUtil.diffDay(YMTDateUtil.getDate("2020-10-23 15:37:50", YMTDateUtil.YMD),
				YMTDateUtil.getDate(YMTDateUtil.getBeforeOrNextDay(0), YMTDateUtil.YMD)));
	}
}